var page = 1;//定义起始page
var depart = $("#depart").find("span");
var area = $("#area").find("span");
var element = $("#element").find("span");
var mode = $("#mode").find("span");
var showCount = 10;
$(function () {
    //debugger/
    page = sessionStorage.getItem('page') == null ? 1 : sessionStorage.getItem('page');
    $(".navChoose").removeClass("navChoose");
    $("#introduce").addClass("navChoose");
    //generateData();
    departMore();
    areaMore();
    elementMore();
    modeMore();
    var typeValue = sessionStorage.getItem('typeValue') == null ? "" : sessionStorage.getItem('typeValue');
    typeSelect(typeValue);
    //typeSelect($("#typeValue").val());
});

depart.each(function (i) {
    $(this).click(function () {
        page = 1;
        sessionStorage.setItem('page', 1);
        //点击的时候将当前点击的值存到session中
        sessionStorage.setItem('depart', depart.eq(i).html());
        depart.attr("class", "select");
        depart.eq(i).attr("class", "selected select bg_img1");
        generateData();
    });
    //如果session里面有值，定义临时变量取到session中的值
    var departs = sessionStorage.getItem('depart') == null ? "" : sessionStorage.getItem('depart');
    //判断临时变量不为空
    if (departs != "") {
        //判断循环的集合中的值是否等于临时变量的值
        if (depart.eq(i).html() == departs) {
            //如果等于设置当前值为选中
            depart.attr("class", "select");
            depart.eq(i).attr("class", "selected select bg_img1");
        }
    }
});
area.each(function (i) {
    $(this).click(function () {
        page = 1;
        sessionStorage.setItem('page', 1);
        //点击的时候将当前点击的值存到session中
        sessionStorage.setItem('area', area.eq(i).html());
        area.attr("class", "select");
        area.eq(i).attr("class", "selected select bg_img1");
        generateData();
    });
    //如果session里面有值，定义临时变量取到session中的值
    var areas = sessionStorage.getItem('area') == null ? "" : sessionStorage.getItem('area');
    //判断临时变量不为空
    if (areas != "") {
        //判断循环的集合中的值是否等于临时变量的值
        if (area.eq(i).html() == areas) {
            //如果等于设置当前值为选中
            area.attr("class", "select");
            area.eq(i).attr("class", "selected select bg_img1");
        }
    }
});
element.each(function (i) {
    $(this).click(function () {
        page = 1;
        sessionStorage.setItem('page', 1);
        //点击的时候将当前点击的值存到session中
        sessionStorage.setItem('element', element.eq(i).html());
        element.attr("class", "select");
        element.eq(i).attr("class", "selected select bg_img1");
        generateData();
    });
    //如果session里面有值，定义临时变量取到session中的值
    var elements = sessionStorage.getItem('element') == null ? "" : sessionStorage.getItem('element');
    //判断临时变量不为空
    if (elements != "") {
        //判断循环的集合中的值是否等于临时变量的值
        if (element.eq(i).html() == elements) {
            //如果等于设置当前值为选中
            element.attr("class", "select");
            element.eq(i).attr("class", "selected select bg_img1");
        }
    }
});
mode.each(function (i) {
    $(this).click(function () {
        page = 1;
        sessionStorage.setItem('page', 1);
        //点击的时候将当前点击的值存到session中
        sessionStorage.setItem('mode', mode.eq(i).html());
        mode.attr("class", "select");
        mode.eq(i).attr("class", "selected select bg_img1");
        generateData();
    });
    //如果session里面有值，定义临时变量取到session中的值
    var modes = sessionStorage.getItem('mode') == null ? "" : sessionStorage.getItem('mode');
    //判断临时变量不为空
    if (modes != "") {
        //判断循环的集合中的值是否等于临时变量的值
        if (mode.eq(i).html() == modes) {
            //如果等于设置当前值为选中
            mode.attr("class", "select");
            mode.eq(i).attr("class", "selected select bg_img1");
        }
    }
});
//$(".menu").hover(function () {
//    $(this).children("div>:first-child").css("visibility", "visible");
//});
$(".prtit_card").hover(function () {
    // $(this).find(".menu").children("div>:first-child").css("visibility", "visible");
}, function () {
    // $(this).find(".menu").children("div>:first-child").css("visibility","hidden");
})

function changeType(flag) {
    page = 1;
    sessionStorage.setItem('page', 1);
    if(flag == $("#typeValue").val()){
        $("#typeValue").val("");
        flag="";
        $("#ptrtit_img1").attr("class", "ptrtit_img1_off");
        $("#ptrtit_img2").attr("class", "ptrtit_img2_off");
        $("#ptrtit_img3").attr("class", "ptrtit_img3_off");
        $("#ptrtit_img4").attr("class", "ptrtit_img4_off");
    }
    typeSelect(flag);
}

function typeSelect(flag){
    sessionStorage.setItem('typeValue', flag);
    switch (flag) {
        case 'N':
            $("#ptrtit_img1").attr("class", "ptrtit_img1_on");
            $("#ptrtit_img2").attr("class", "ptrtit_img2_off");
            $("#ptrtit_img3").attr("class", "ptrtit_img3_off");
            $("#ptrtit_img4").attr("class", "ptrtit_img4_off");
            $("#typeValue").val("N");
            break;
        case 'A':
            $("#ptrtit_img1").attr("class", "ptrtit_img1_off");
            $("#ptrtit_img2").attr("class", "ptrtit_img2_on");
            $("#ptrtit_img3").attr("class", "ptrtit_img3_off");
            $("#ptrtit_img4").attr("class", "ptrtit_img4_off");
            $("#typeValue").val("A");
            break;
        case 'R':
            $("#ptrtit_img1").attr("class", "ptrtit_img1_off");
            $("#ptrtit_img2").attr("class", "ptrtit_img2_off");
            $("#ptrtit_img3").attr("class", "ptrtit_img3_on");
            $("#ptrtit_img4").attr("class", "ptrtit_img4_off");
            $("#typeValue").val("R");
            break;
        case 'D':
            $("#ptrtit_img1").attr("class", "ptrtit_img1_off");
            $("#ptrtit_img2").attr("class", "ptrtit_img2_off");
            $("#ptrtit_img3").attr("class", "ptrtit_img3_off");
            $("#ptrtit_img4").attr("class", "ptrtit_img4_on");
            $("#typeValue").val("D");
            break;
        default:
            break;
    }
    generateData();
}

function generateData() {
    var area = $("#area").find(".selected").eq(0).text();
    var element = $("#element").find(".selected").eq(0).text();
    var depart = $("#depart").find(".selected").eq(0).text();
    var mode = $("#mode").find(".selected").eq(0).text();
    var productType = $("#typeValue").val();
    if (area == "全部" || area == undefined) {
        area = "";
    }
    if (element == "全部" || element == undefined) {
        element = "";
    }
    if (depart == "全部" || depart == undefined) {
        depart = "";
    }
    if (mode == "全部" || mode == undefined) {
        mode = "";
    }
    getProduct(productType, depart, area, element, mode);
}

function getProduct(productType, depart, oceanArea, element, mode) {
    $.ajax({
        type: "post",
        async: true,
        url: "/home/productBrowsing/getProduct",
        data: {
            "productType": productType,
            "oceanArea": oceanArea,
            "element": element,
            "depart": depart,
            "mode": mode,
            "currentPage": page
        },
        dataType: "json",        //返回数据形式为json
        success: function (data) {
            var foreresult = data.data
            result = foreresult.datas;
            for (var i = 0; i < 12; i++) {
                if (result && i < result.length) {
                    /*console.log(result[i]);*/
                    $(".prtit_card").eq(i).css("display", "block");
                    $(".prtit_ctop").eq(i).text(result[i].productName);
                    $(".prtit_ctop").eq(i).attr("title", result[i].productName);
                    $(".yaosu1").eq(i).nextAll(".yaosu_xinx").text(result[i].element);
                    $(".yaosu1").eq(i).nextAll(".yaosu_xinx").attr("title", empty(result[i].element));
                    $(".yaosu2").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].area));
                    $(".yaosu2").eq(i).nextAll(".yaosu_xinx").attr("title", empty(result[i].area));
                    $(".yaosu3").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].resolution));
                    $(".yaosu3").eq(i).nextAll(".yaosu_xinx").attr("title", empty(result[i].resolution));
                    $(".yaosu4").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].aging));
                    $(".yaosu4").eq(i).nextAll(".yaosu_xinx").attr("title", empty(result[i].aging));
                    $(".yaosu5").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].fileType));
                    $(".yaosu5").eq(i).nextAll(".yaosu_xinx").attr("title", empty(result[i].fileType));
                    $(".yaosu6").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].frequency));
                    $(".yaosu6").eq(i).nextAll(".yaosu_xinx").attr("title", empty(result[i].frequency));
                    if(result[i].productName == '智能网格指导产品'){
                        $(".prtit_card").find("a").eq(i * 3).attr("onclick", 'ltypetz()');
                    }else{
                        $(".prtit_card").find("a").eq(i * 3).attr("onclick", 'loadhref("'+"/home/productBrowsing/detail?productId=" + result[i].productId+'")');
                    }
                    console.log("传参1")
                    $(".prtit_card").find("a").eq(i * 3 + 1).attr("onclick", "loadhref('"+"/home/productBrowsing/subscription/" + result[i].productId+"')");
                    $(".prtit_card").find("a").eq(i * 3 + 2).attr("onclick", 'loadhref("'+"/home/productBrowsing/detail?productId=" + result[i].productId+'")');
                } else {
                    $(".prtit_card").eq(i).css("display", "none");
                }
            }
            var pageCount = foreresult.pageCount; //取到pageCount的值(把返回数据转成object类型)
//            var currentPage = data[0].currentPage; //得到currentPage
            this.page = foreresult.currentPage; //得到currentPage

            var options = {
                bootstrapMajorVersion: 3, //版本
//                currentPage: currentPage, //当前页数
                currentPage: page, //当前页数
                totalPages: pageCount, //总页数
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    }
                },//点击事件，用于通过Ajax来刷新整个list列表
                onPageClicked: function (event, originalEvent, type, page) {
                    sessionStorage.setItem('page', page);
                    $.ajax({
                        url: "/home/productBrowsing/getProduct",
                        type: "post",
                        data: {
                            "productType": productType,
                            "oceanArea": oceanArea,
                            "element": element,
                            "depart": depart,
                            "mode": mode,
                            "currentPage": page
                        },
                        success: function (data) {
                            result = data.data.datas;
                            for (var i = 0; i < 12; i++) {
                                if (result && i < result.length) {
                                    $(".prtit_card").eq(i).css("display", "block");
                                    $(".prtit_ctop").eq(i).text(result[i].productName);
                                    $(".yaosu1").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].element));
                                    $(".yaosu2").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].area));
                                    $(".yaosu3").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].resolution));
                                    $(".yaosu4").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].aging));
                                    $(".yaosu5").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].fileType));
                                    $(".yaosu6").eq(i).nextAll(".yaosu_xinx").text(empty(result[i].frequency));
                                    $(".prtit_card").find("a").eq(i * 3).attr("onclick", "detail('"+"/home/productBrowsing/detail?productId=" + result[i].productId+"')");
                                    console.log("传参2")
                                    $(".prtit_card").find("a").eq(i * 3 + 1).attr("onclick", "loadhref('"+"/home/productBrowsing/subscription/" + result[i].productId+"')");
                                    $(".prtit_card").find("a").eq(i * 3 + 2).attr("onclick", "detail('"+"/home/productBrowsing/detail?productId=" + result[i].productId+"')");
                                } else {
                                    $(".prtit_card").eq(i).css("display", "none");
                                }
                            }
                        }
                    });
                }
            };
            $('#example').bootstrapPaginator(options);
        }
    });
}

function ltypetz(){
    $("#test").load("/home/productBrowsing/ltype");//如果是智能网格指导产品，则跳转到智能网格指导产品
}
/**
 * 空值返回未知
 * @param value
 * @returns {*}
 */
function empty(value) {
    if (value == null || typeof value == undefined || typeof value == "null") {
        return "未设定";
    }

    if (typeof value == "string" && value == "") {
        return "未设定";
    }
    return value;
}
var prefix = "/home/productBrowsing"
function isPreview(productId) {
    $.ajax({
        url: prefix+"/isPreview",
        type: "post",
        async: true,
        data: {
            "productId": productId
        },
        dataType: "json",
        success: function (data) {
            if (data.success) {
                window.location.href = prefix+"/toPreview?productId=" + productId;
            } else {
                //doc、docx、ppt、pptx、xls、xlsx、txt、pdf
                alert("仅支持 word、ppt、txt、excel 等格式预览");
            }
        }
    });
}
var departFlag = true;
function departMore() {
    if (departFlag) {
        for (var i = showCount; i < depart.length; i++) {
            depart.eq(i).css("display", "none");
        }

        $(".departFlag").removeClass("prtit_bt1_choose");
        $(".departFlag").addClass("prtit_bt1_normal");
        departFlag = false;
    } else {
        for (var i = showCount; i < depart.length; i++) {
            depart.eq(i).css("display", "");
        }

        $(".departFlag").removeClass("prtit_bt1_normal");
        $(".departFlag").addClass("prtit_bt1_choose");
        departFlag = true;
    }
}
var areaFlag = true;
function areaMore() {
    if (areaFlag) {
        for (var i = showCount; i < area.length; i++) {
            area.eq(i).css("display", "none");
        }

        $(".areaFlag").removeClass("prtit_bt1_choose");
        $(".areaFlag").addClass("prtit_bt1_normal");
        areaFlag = false;
    } else {
        for (var i = showCount; i < area.length; i++) {
            area.eq(i).css("display", "");
        }

        $(".areaFlag").removeClass("prtit_bt1_normal");
        $(".areaFlag").addClass("prtit_bt1_choose");
        areaFlag = true;
    }
}
var elementFlag = true;
function elementMore() {
    if (elementFlag) {
        for (var i = showCount; i < element.length; i++) {
            element.eq(i).css("display", "none");
        }
        elementFlag = false;
        $(".elementFlag").removeClass("prtit_bt1_choose");
        $(".elementFlag").addClass("prtit_bt1_normal");
    } else {
        for (var i = showCount; i < element.length; i++) {
            element.eq(i).css("display", "");
        }
        $(".elementFlag").removeClass("prtit_bt1_normal");
        $(".elementFlag").addClass("prtit_bt1_choose");
        elementFlag = true;
    }
}
var modeFlag = true;
function modeMore() {
    if (modeFlag) {
        for (var i = showCount; i < mode.length; i++) {
            mode.eq(i).css("display", "none");
        }
        modeFlag = false;
        $(".modeFlag").removeClass("prtit_bt1_choose");
        $(".modeFlag").addClass("prtit_bt1_normal");
    } else {
        for (var i = showCount; i < mode.length; i++) {
            mode.eq(i).css("display", "");
        }
        $(".modeFlag").removeClass("prtit_bt1_normal");
        $(".modeFlag").addClass("prtit_bt1_choose");
        modeFlag = true;
    }
}
